﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using NLog;

namespace Tab2Sql
{
	/// <summary>
	/// If the user holds the ALT key the CLEAR button changes to ARCHIVE which can be used to 
	/// log the current program state to an XML log file. This class is the serializable object
	/// used for these purposes.
	/// </summary>
    [XmlRoot("History")]
    public class Archiver
    {
		private static Logger logger = LogManager.GetCurrentClassLogger ();

        /// <summary>
        ///
        /// </summary>
        [XmlAttribute("LogUID")]
        public Guid LogID { get; set; }
        /// <summary>
        ///
        /// </summary>
        public DateTime Timestamp { get; set; }
        /// <summary>
        ///
        /// </summary>
        public String TheDate { get; set; }
        /// <summary>
        ///
        /// </summary>
        [XmlElement("Path")]
        public String ProgramPath { get; set; }
        /// <summary>
        ///
        /// </summary>
        public String ProgramVersion { get; set; }
        /// <summary>
        ///
        /// </summary>
        public String AssemblyVersion { get; set; }
        /// <summary>
        ///
        /// </summary>
        public String UserName { get; set; }
        /// <summary>
        ///
        /// </summary>
        public String ComputerName { get; set; }
        /// <summary>
        ///
        /// </summary>
        public String KeyWords { get; set; }
        /// <summary>
        ///
        /// </summary>
        public String MemoryInUse { get; set; }
        /// <summary>
        ///
        /// </summary>
        [XmlElement("Input")]
        public String InBox { get; set; }
        /// <summary>
        ///
        /// </summary>
        [XmlElement("Output")]
        public String OutBox { get; set; }
        /// <summary>
        ///
        /// </summary>
        public Archiver()
        {
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="inputBox">
        /// A <see cref="Tab2Sql.inRtbox"/>
        /// </param>
        /// <param name="outputBox">
        /// A <see cref="System.Windows.Forms.RichTextBox"/>
        /// </param>
        public Archiver(Tab2Sql.inRtbox inputBox, System.Windows.Forms.RichTextBox outputBox)
        {
            
            ProgramVersion = "Build "+Properties.Settings.Default.Build_Number.ToString()+" - "+Properties.Settings.Default.Build_Date.ToShortDateString();
            Assemblage asm = new Assemblage();
            AssemblyVersion = asm.AssemblyVersion;
            asm = null;
            if (inputBox.TableName.Length > 0)
            {
                KeyWords = inputBox.TableName;
            }
            MemoryInUse  = DGen.DText.FormatBytes(GC.GetTotalMemory(false));
            InBox = DGen.DText.Left(inputBox.Text, 1000000);
            OutBox =DGen.DText.Left(outputBox.Text,1000000);
            
            LogID = Guid.NewGuid();
            Timestamp = DateTime.Now;
            TheDate = Timestamp.ToShortDateString();
            ComputerName = Environment.MachineName;
            UserName = Environment.UserName;
            ProgramPath = Environment.CommandLine;

        }
       /// <summary>
       ///
       /// </summary>
        public void WriteXML ()
        {
        	try
            {
        		XmlSerializer serializer;
        		TextWriter writer = new StreamWriter ("History.xml", true);
        		serializer = new XmlSerializer (typeof(Archiver));
        		
                serializer.Serialize (writer, this);
        		writer.Close ();
        	}
            catch (Exception ex)
            {
        		System.Windows.Forms.MessageBox.Show (ex.Message);
        		logger.Error (ex.Message);
				
            }
        } 
    }


    
}
